package com.restaurant.serviceimpl;

import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.restaurant.domain.Menuorder;
import com.restaurant.service.MenuorderService;

public class MenuorderServiceImpl implements MenuorderService {

   private HibernateTemplate hibernateTemplate;

   @Autowired
   public void setSessionFactory(SessionFactory sessionFactory){
       this.hibernateTemplate = new HibernateTemplate(sessionFactory);
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Menuorder addOrModify( Menuorder objMenuorder ) {
       return ( Menuorder ) hibernateTemplate.merge( objMenuorder );
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Menuorder deleteRecord( Menuorder objMenuorder ) {
       hibernateTemplate.delete( objMenuorder );
       return objMenuorder;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Menuorder modifyRecord( Menuorder objMenuorder ) {
       return ( Menuorder ) hibernateTemplate.merge( objMenuorder );
   }

   @SuppressWarnings("unchecked")
   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public List<Menuorder> loadAll() {
       return (List<Menuorder>)hibernateTemplate.findByNamedQuery( "Menuorder.findAll");
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Menuorder loadByPrimaryKey( Long primaryKey ) {
       return (Menuorder) hibernateTemplate.get( Menuorder.class, primaryKey );
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Menuorder deleteByPrimaryKey( Long primaryKey ) {
       return deleteRecord(loadByPrimaryKey(primaryKey));
   }

   @SuppressWarnings("unchecked")
   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public List<Menuorder> loadByHql( String query ) {
       return ( List<Menuorder> ) hibernateTemplate.find( query );
   }

	@Override
	public double getSumMenuPrice(Long tableorderID) {
		//Transaction tx = hibernateTemplate.getSessionFactory().openSession().beginTransaction();
		//double amount = (Double) hibernateTemplate.getSessionFactory().openSession().createQuery("SELECT SUM(m.menu.price) FROM Menuorder m WHERE m.tableorder.tableorderID = :tableorderID").setParameter("tableorderID", tableorderID).uniqueResult();
		//tx.commit();
		double amount = (Double) hibernateTemplate.find("SELECT SUM(m.menu.price) FROM Menuorder m WHERE m.tableorder.tableorderID =" + tableorderID).get(0);
		return amount;
	}	
}